package com.higgins.eventtracking.client.health.record.rolling;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.GoalDTO;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;
import com.higgins.eventtracking.client.health.components.WeekNumberListBox;
import com.higgins.eventtracking.client.health.record.RulesColumns;
import com.higgins.eventtracking.client.health.record.RulesTable;

public class AchievementRollingSubTab extends VerticalPanel 		{
	private ScrollPanel achievementsScrollPanel = null;
	private final EventTrackingServiceAsync greetingService = GWT
														.create(EventTrackingService.class);
	private VerticalPanel panel = null;
	private TextBox startDate = null;
	private TextBox endDate = null;
	private TextBox intensity = null;
	private TextBox frameOfMind = null;
	private Button filterButton = null;
	private boolean firstRun = true;
	private GenericTabInterface parentTab = null;
    private String dateFormat = "dd MMM yyyy";
    private Panel filterPanel = null;
	
	public AchievementRollingSubTab(GenericTabInterface parentTab)	{
		this.parentTab = parentTab;
		filterPanel = setUpFilterPanel();
	}
	private Panel setUpFilterPanel()	{
		HorizontalPanel filterPanel = new HorizontalPanel();
		startDate = new TextBox();
		startDate.setText(convertDateToString(getDateLastWeek()));
		endDate = new TextBox();
		endDate.setText(convertDateToString(getEndOfToday()));
		intensity = new TextBox();
		frameOfMind = new TextBox();
		filterButton = new Button("Filter");
		filterButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                update();
            } });
		filterPanel.add(startDate);
		filterPanel.add(endDate);
		filterPanel.add(intensity);
		filterPanel.add(frameOfMind);
		filterPanel.add(filterButton);
		return filterPanel;
	}
	private Date getDateLastWeek()    {
	    long theFuture = System.currentTimeMillis() - (86400 * 7 * 1000);
	    Date lastWeek = new Date(theFuture);
	    return lastWeek;
	}
    private Date getEndOfToday()    {
        long theFuture = System.currentTimeMillis() + (86400 * 1 * 1000);
        Date lastWeek = new Date(theFuture);
        return lastWeek;
    }
    protected String convertDateToString(Date inValue){
        DateTimeFormat format = DateTimeFormat.getFormat(dateFormat);
        return format.format(inValue);
    }   
	
	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
        clear();
        setHeight("20em");
//      populateGoals();
        panel = new VerticalPanel();
        panel.add(filterPanel);
		achievementsScrollPanel = new ScrollPanel();
		achievementsScrollPanel.add(panel);
		achievementsScrollPanel.setHeight("100em");
        populateAchievements();
		add(achievementsScrollPanel);
//		firstRun = false;
	}
	private Date getStartDate()    {
        String string = startDate.getText();
        Long longDate = Date.parse(string);
        Date newDate = new Date(longDate);
        return newDate;
	}
	private Date getEndDate()  {
        String string = endDate.getText();
        Long longDate = Date.parse(string);
        Date newDate = new Date(longDate);
        return newDate;
	}

   private void populateAchievements() {
        greetingService.retrieveAchievements(getStartDate(), getEndDate(), intensity.getText(), frameOfMind.getText(), new AsyncCallback<List<String>>()   {
            @Override
            public void onFailure(Throwable caught) {
            }
            @Override
            public void onSuccess(List<String> results) {
                addAchievements(results);
            }
        });     
    }

   private void addAchievements(List<String> results)  {
       try {
           achievementsScrollPanel.remove(panel);
       } catch (Exception e)   {
           
       }
       panel = new VerticalPanel();
       panel.add(filterPanel);
       for (String result:results) {
           Label label = new Label(result);
           panel.add(label);
           panel.setCellHeight(label, Integer.toString(label.getElement().getOffsetHeight())+"px");
       }
       panel.setHeight(String.valueOf(results.size())+ "em");
       achievementsScrollPanel.add(panel);
       achievementsScrollPanel.setHeight("15em");
   }
}
